home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5504 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Got Questions About Multi-Dimension Arrays (After Reading FAQ)
  5. Date: 1 Feb 1996 01:18:41 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.823155385@pegasus.montclair.edu>
  8. References: <4ep87b$o60@alcor.usc.edu>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12.  
  13. ** Quoting a message by <wawda@alcor.usc.edu> dated <31-Jan-1996>:
  14.  
  15. >     why it isn't possible to do this:
  16. >
  17. > void myfunc(int *array,int rows,int cols)
  18. > {
  19. >    printf("%d\n",array[1][1]);
  20. > }
  21.  
  22.     int *array is a pointer to an integer, ONE integer.  You can't use
  23. the []'s on an integer, so the compiler will complain about it.  Doesn't
  24. matter if the name of your formal parameter (in the function heading)
  25. matches a global variable.. as far as C is concerned, this function
  26. expects a pointer to an integer, and two ints.
  27.  
  28. >
  29. > and call the function like this:
  30. >
  31. > int m[2][2] = {1, 2, 3, 4};
  32.  
  33.     I always m[2][2] = { {1, 2}, {3, 4} }; else it gets very confusing.
  34. >
  35. > myfunc(m,2,2);
  36.  
  37.     Since the array name m devolves into a pointer to an int which is
  38. the first element of the array, *array = 1 in the function when called
  39. in this way.
  40.  
  41. > So why can't C convert from pointer to integer to a multidemsional
  42. > array. The reason I want to do this is because my function needs to
  43. > take in an array of any width and height. Thank you for your time,
  44.  
  45.     A dynamic array huh?  You need to think in pointers (note the
  46. plurality) Arrays are really pointers, the more dimensions, the more
  47. pointers you need.
  48.  
  49.     Try,
  50.  
  51. : void yourfunc(int *array[], int rows, int cols)
  52.  
  53.     The (int *)array can be indexed with one [] index in your function,
  54. and then you can use the other [] to index rows (or is it columns?) :)
  55. What it really comes down to is int **array, a double pointer!  Think
  56. about it.  C is full of hobgoblins, most are more gruesome though.
  57.  
  58.                                           -- Stone
  59. --
  60. ... Engineers never die, they just lose their tolerance.
  61.  
  62.  
  63.